﻿using System.Deployment.Application;
using System.IO;
using System;

namespace DevTools.Common.Components
{
    public static class ClickOnceDataFileHelper
    {
        public static bool Exists(string relativePath)
        {
            string path = GetPath(relativePath);

            return File.Exists(path);
        }

        public static string[] ReadAllLines(string relativePath)
        {
            string path = GetPath(relativePath);

            return File.ReadAllLines(path);
        }

        public static string ReadAllText(string relativePath)
        {
            string path = GetPath(relativePath);

            return File.ReadAllText(path);
        }

        public static void WriteAllLines(string relativePath, string[] lines)
        {
            string path = GetPath(relativePath);

            File.WriteAllLines(path, lines);
        }

        public static void WriteAllText(string relativePath, string content)
        {
            string path = GetPath(relativePath);

            File.WriteAllText(path, content);
        }

        public static byte[] ReadAllBytes(string relativePath)
        {
            string path = GetPath(relativePath);

            return File.ReadAllBytes(path);
        }

        public static void WriteAllBytes(string relativePath, byte[] bytes)
        {
            string path = GetPath(relativePath);

            File.WriteAllBytes(path, bytes);
        }

        private static string GetPath(string relativePath)
        {
            if (ApplicationDeployment.IsNetworkDeployed)
                return ApplicationDeployment.CurrentDeployment.DataDirectory + "\\" + relativePath;
            else
                    return Path.Combine(Environment.CurrentDirectory, relativePath);
        }
    }
}